﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Console
{
    public partial class Form3 : Form
    {
        // 键盘捕获到的字母
        private volatile char keyPressed = '\0';

        // 串口
        SerialPort serialPort;

        // 当前X轴速度
        private byte vX = 0x00;

        // 计时器
        System.Windows.Forms.Timer time;

        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += Form3_KeyDown;

            label3.AutoSize = true;
            label3.Left = (ClientSize.Width - 113) / 2;
            label1.Left = label3.Left;
            label2.Left = label3.Left;

            time = new System.Windows.Forms.Timer();
            time.Interval = 1000; // 每隔1秒触发一次
            time.Tick += getTime;
            time.Start();



            Task controlCarTask = Task.Run(() =>
            {
                // 打开串口
                try
                {
                    serialPort = new SerialPort(SerialPort.GetPortNames()[0], 115200);
                    serialPort.Open();
                }
                catch (Exception)
                {
                    System.Console.WriteLine("找不到串口设备");
                    MessageBox.Show("找不到串口设备");
                    return;
                }

                while (true)
                {
                    // 在这里编写遥控小车的代码
                    {
                        // 处理按键输入
                        switch (keyPressed)
                        {
                            case 'W':
                                // 在这里编写处理 W 键的代码
                                System.Console.WriteLine("WWWWWPPPPP");
                                byte[] frameW = GenerateBytes(1, vX, 0, 0);
                                serialPort.Write(frameW, 0, 12);

                                // 委托：更新GUI界面
                                Invoke(new MethodInvoker(() =>
                                {
                                    label1.Text = "运动轨迹：" + "W";
                                }));

                                // 恢复初始发送状态
                                keyPressed = '\0';
                                break;
                            case 'A':
                                // 在这里编写处理 A 键的代码
                                System.Console.WriteLine("AAAAA");
                                byte[] frameA = GenerateBytes(0, 0, 1, 20);
                                serialPort.Write(frameA, 0, 12);

                                // 委托：更新GUI界面
                                Invoke(new MethodInvoker(() =>
                                {
                                    label1.Text = "运动轨迹：" + "A";
                                }));

                                // 恢复初始发送状态
                                keyPressed = '\0';
                                break;
                            case 'S':
                                // 在这里编写处理 S 键的代码
                                System.Console.WriteLine("SSSSS");
                                byte[] frameS = GenerateBytes(0, vX, 0, 0);
                                serialPort.Write(frameS, 0, 12);

                                // 委托：更新GUI界面
                                Invoke(new MethodInvoker(() =>
                                {
                                    label1.Text = "运动轨迹：" + "S";
                                }));

                                // 恢复初始发送状态
                                keyPressed = '\0';
                                break;
                            case 'D':
                                // 在这里编写处理 D 键的代码
                                System.Console.WriteLine("DDDDD");
                                byte[] frameD = GenerateBytes(0, 0, 0, 20);
                                serialPort.Write(frameD, 0, 12);

                                // 委托：更新GUI界面
                                Invoke(new MethodInvoker(() =>
                                {
                                    label1.Text = "运动轨迹：" + "D";
                                }));

                                // 恢复初始发送状态
                                keyPressed = '\0';
                                break;
                            case 'R':
                                // 在这里编写处理 R 键的代码
                                System.Console.WriteLine("RRRRR");

                                // 增加档位
                                if (vX <= 0x50)
                                    vX = (byte)(vX + 0x14);
                                Invoke(new MethodInvoker(() =>
                                {
                                    // 在这里编写更新 GUI 界面的代码
                                    label2.Text = "运动速度：" + vX.ToString();
                                }));
                                keyPressed = '\0';
                                break;
                            case 'F':
                                // 在这里编写处理 F 键的代码
                                System.Console.WriteLine("FFFFF");

                                // 减少档位
                                if (vX >= 0x14)
                                    vX = (byte)(vX - 0x14);
                                Invoke(new MethodInvoker(() =>
                                {
                                    // 在这里编写更新 GUI 界面的代码
                                    label2.Text = "运动速度：" + vX.ToString();
                                }));
                                keyPressed = '\0';
                                break;
                        }
                    }
                }

            });
        }

        private void Form3_KeyDown(object sender, KeyEventArgs e)
        {
            // 判断按下的键是否为 WASD 键
            switch (e.KeyCode)
            {
                case Keys.W:
                    keyPressed = 'W';
                    break;
                case Keys.A:
                    keyPressed = 'A';
                    break;
                case Keys.S:
                    keyPressed = 'S';
                    break;
                case Keys.D:
                    keyPressed = 'D';
                    break;
                case Keys.R:
                    keyPressed = 'R';
                    break;
                case Keys.F:
                    keyPressed = 'F';
                    break;
            }
        }

        byte[] GenerateBytes(int xD, int xV, int zD, int zV)
        {
            byte[] bytes = new byte[12];
            bytes[0] = 0x21;
            bytes[1] = 0x21;
            bytes[2] = 0x0A;
            bytes[3] = 0x00;
            bytes[4] = (byte)((xD > 0) ? 0x01 : 0x00);
            bytes[5] = (byte)xV;
            bytes[6] = 0x00;
            bytes[7] = 0x00;
            bytes[8] = (byte)((zD > 0) ? 0x01 : 0x00);
            bytes[9] = (byte)zV;
            short sum = 0;
            for (int i = 0; i < 10; i++)
            {
                sum = (short)(sum + (short)(bytes[i]));
            }

            bytes[10] = (byte)(sum & 0xFF);
            bytes[11] = (byte)(sum >> 8);
            return bytes;
        }

        private void getTime(object sender, EventArgs e)
        {
            label3.Text = "当前时间：" + DateTime.Now.ToString("HH:mm:ss");
            //Console.WriteLine(label3.Width);
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            time.Stop();
        }
    }
}
